package com.example.mqproduct.product;

import com.example.common.config.Demo01Configuration;
import com.example.common.message.Demo01Message;
import com.example.common.message.Demo02Message;
import com.example.common.message.Demo03Message;
import com.example.common.message.Demo05Message;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.BatchingRabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Log4j2
@Component
public class Demo01Product {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    BatchingRabbitTemplate batchingRabbitTemplate;

    public void sendMsg(int id) {
        Demo01Message msg = new Demo01Message();
        msg.setId(id);

        rabbitTemplate.convertAndSend(Demo01Configuration.EXCHANGE, Demo01Configuration.ROUTING_KEY, msg);

        log.info("往MQ中发送数据:" + id);
    }

    public void sendMsg2(Integer id, String routingKey) {
        // 创建 Demo02Message 消息
        Demo02Message message = new Demo02Message();
        message.setId(id);
        // 同步发送消息
        rabbitTemplate.convertAndSend(Demo02Message.EXCHANGE, routingKey, message);

        log.info("往MQ中发送数据:" + id);

    }


    public void sendMsg3(Integer id) {
        // 创建 Demo03Message 消息
        Demo03Message message = new Demo03Message();
        message.setId(id);
        // 同步发送消息
        rabbitTemplate.convertAndSend(Demo03Message.EXCHANGE, null, message);

        log.info("往MQ中发送数据:" + id);
    }


    public void sendMsg5(Integer id) {
        // 创建 Demo05Message 消息
        Demo05Message message = new Demo05Message();
        message.setId(id);
        // 同步发送消息
        batchingRabbitTemplate.convertAndSend(Demo05Message.EXCHANGE, Demo05Message.ROUTING_KEY, message);

//        batchingRabbitTemplate.flush();

        log.info("往MQ中发送数据:" + id);

    }


    @Async
    public void asyncSendMsg(int id) {
        this.sendMsg(id);
    }
}
